# -*- coding: utf-8 -*-
import requests
from lxml import etree
from tools.mysqlconn import NBAdb
from mysql_text import sql_insert_text
from tools.auxiliary_tool import normalization
import re


class Player:

    def __init__(self):
        """
        初始化一些东西
        """
        # 提取team参数
        one = NBAdb()
        select_sql = """
        SELECT bi_English_name
        FROM team_brief_introduction
        ORDER BY bi_id;
        """
        list_one = one.select_data(select_sql)
        self.list_team = []
        for item in list_one:
            for i in item:
                self.list_team.append(i[-4:-1])
        # print(self.list_team)
        # 释放资源
        del one

        # 初始化请求头
        self.headers = {
            'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/89.0.4389.90 Safari/537.36'
        }

    def get_player_regular_data(self):
        """
        获取历年常规赛效力球员数据、历年常规赛效力球员汇总数据；
        2个表。
        :return:
        """
        two = NBAdb()

        # 提取season参数（建立在提取team参数的前提上）
        for team in self.list_team:
            url = 'http://www.stat-nba.com/team/{}.html'.format(team)
            res = requests.get(url=url, headers=self.headers)
            res.encoding = 'utf-8'
            html = etree.HTML(res.text)

            content_one = html.xpath('//div[@class="superstarList"]/div[1]/div[2]/a/@id')
            for a in content_one:
                season = (re.findall(r'\d+', a))[0]

                # 建立参数
                params = {
                    'team': team,
                    'season': season,
                    'col': 'pts',
                    'order': '1',
                    'isseason': '1'  # isseason = 1 代表了常规赛
                }
                url_season = 'http://www.stat-nba.com/team/stat_box_team.php'
                res_season = requests.get(url_season, params=params, headers=self.headers)
                res_season.encoding = 'utf-8'
                html_season = etree.HTML(res_season.text)

                # 历年常规赛效力球员数据
                content_two = html_season.xpath('//table[@class="stat_box"]/tbody/tr[@class="sort"]')
                for i in content_two:
                    list_zj = [team, season]
                    list_ls = i.xpath('.//td//text()')
                    list_zj.extend(list_ls)
                    two.insert_table(sql_insert_text.insert_team_player_date_regular, list_zj)

                # 历年常规赛效力球员汇总数据
                content_three = html_season.xpath(
                    '//table[@class="stat_box"]/tbody/tr/td[contains(text(),"全队数据")]/../td/text()')
                if content_three:
                    list_zj = [team, season]
                    list_zj.extend(content_three)
                    two.insert_table(sql_insert_text.insert_team_player_count_data_regular, list_zj)

        # 释放资源
        del two

    def get_player_playoff_data(self):
        """
        获取历年季后赛效力球员数据、历年季后赛效力球员汇总数据；
        2个表。
        :return:
        """
        three = NBAdb()

        # 提取season参数（建立在提取team参数的前提上）
        for team in self.list_team:
            url = 'http://www.stat-nba.com/team/{}.html'.format(team)
            res = requests.get(url=url, headers=self.headers)
            res.encoding = 'utf-8'
            html = etree.HTML(res.text)

            content_one = html.xpath('//div[@class="superstarList"]/div[1]/div[2]/a/@id')
            for a in content_one:
                season = (re.findall(r'\d+', a))[0]

                # 建立参数
                params = {
                    'team': team,
                    'season': season,
                    'col': 'pts',
                    'order': '1',
                    'isseason': '0'  # isseason = 0 代表了季后赛
                }
                url_season = 'http://www.stat-nba.com/team/stat_box_team.php'
                res_season = requests.get(url_season, params=params, headers=self.headers)
                res_season.encoding = 'utf-8'
                html_season = etree.HTML(res_season.text)

                if html_season.xpath('//table[@class="stat_box"]/tbody/tr'):
                    # 历年季后赛效力球员数据
                    content_two = html_season.xpath('//table[@class="stat_box"]/tbody/tr[@class="sort"]')
                    for i in content_two:
                        list_zj = [team, season]
                        list_ls = i.xpath('.//td//text()')
                        list_zj.extend(list_ls)
                        three.insert_table(sql_insert_text.insert_team_player_date_playoff, list_zj)

                    # 历年季后赛效力球员汇总数据
                    content_three = html_season.xpath(
                        '//table[@class="stat_box"]/tbody/tr/td[contains(text(),"全队数据")]/../td/text()')
                    if content_three:
                        list_zj = [team, season]
                        list_zj.extend(content_three)
                        three.insert_table(sql_insert_text.insert_team_player_count_data_playoff, list_zj)

        # 释放资源
        del three


if __name__ == '__main__':
    print('ok')
